<?php
namespace app\teacher\controller;

use app\teacher\model\Course;
use app\teacher\model\Teacher;
use app\teacher\model\Text;
use app\teacher\model\User;
use app\teacher\model\Apply;
use think\Request;
use think\Session;
class  My extends Base
{
    //我的
    public function index(){
        $user = $this->user_info();
        //统计总数
      $all_count = [
        "xxdsh"=>0,
        "dzf"=>0,
        "jfdsh"=>0,
        "lq"=>0,
      ];
        //统计自己的数据
         $count = collection(Apply::where(["teacher_id"=>$user['id'],"grade"=>date("Y",time())])->group("status")->field("count(*) as c,status")->select())->toArray();
          $my_count = [
            "xxdsh"=>0,
            "dzf"=>0,
            "jfdsh"=>0,
            "lq"=>0,
          ];
          foreach($count as $val){
            switch($val["status"]){
              case 1 : $my_count['jfdsh'] = $val["c"];$all_count['jfdsh'] += $val["c"]; break;
              case 2 : $my_count['lq'] = $val["c"];$all_count['lq'] += $val["c"]; break;
              case 4 : $my_count['dzf'] = $val["c"];$all_count['dzf'] += $val["c"]; break;
              case 5 : $my_count['xxdsh'] = $val["c"];$all_count['xxdsh'] += $val["c"]; break;
            }
          }
        //统计自己和自己老师的数据
        $my_teacher_count = collection(Teacher::where(["pid"=>$user['id']])->field("id,name")->select())->toArray();

        //总数据
        foreach ($my_teacher_count as $key=>$val) {
          //统计自己的数据
          $counts = collection(Apply::where(["teacher_id" => $val['id'], "grade" => date("Y", time())])->group("status")->field("count(*) as c,status")->select())->toArray();
          $my_teacher_count[$key]['count'] = [
            "xxdsh" => 0,
            "dzf" => 0,
            "jfdsh" => 0,
            "lq" => 0,
          ];
          foreach ($counts as $val) {
            switch ($val["status"]) {
              case 1 :
                $my_teacher_count[$key]['count']['jfdsh'] = $val["c"];
                $all_count['jfdsh'] += $val["c"];
                break;
              case 2 :
                $my_teacher_count[$key]['count']['lq'] = $val["c"];
                $all_count['lq'] += $val["c"];
                break;
              case 4 :
                $my_teacher_count[$key]['count']['dzf'] = $val["c"];
                $all_count['dzf'] += $val["c"];
                break;
              case 5 :
                $my_teacher_count[$key]['count']['xxdsh'] = $val["c"];
                $all_count['xxdsh'] += $val["c"];
                break;
            }
          }
        }
        //整理数据
        $data = array(
            "setting" => $this->getAllSetting(),
            "user"=>$user->toArray(),
            "my_count"=>$my_count,
            "myteachercount"=>$my_teacher_count,
            "all_count"=>$all_count
        );
        return view("index",$data);
    }
    //个人信息
    public function info(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "user"=>$user,
        );
        return view("info",$data);
    }
    //我的学生
    public function my_student(Request $request){
        $user = $this->user_info(); //当前登录的老师
        $id = $request->get("id"); //接收老师id
        //如果接收的老师的id与登录的用户id不一致就查询用户的信息
        $th='';
        if($user['id'] != $id){
          $th = Teacher::where("id",$id)->field("name")->find();
        }
        $Applys = collection(Apply::where(["teacher_id"=>$id,"grade"=>date("Y",time())])->order("create_time","desc")->select())->toArray();
        foreach($Applys as $key=>$val){
          $Applys[$key]['user']=User::where(["id"=>$val['userid']])->find()->toArray();
        }
        $data = array(
          "setting" => $this->getAllSetting(),
          "apply" => $Applys,
          "th"=>$th
        );
        return view("my_student", $data);
    }
    /*
     * 查看学生的详细信息
     */
    public function studentinfo(Request $request){
      $teacher = $this->user_info(); //当前登录的老师
      $id = $request->get("id");
      $apply = Apply::where(["userid"=>$id])->find();
      $user = User::get($id);

      $data = array(
        "setting" => $this->getAllSetting(),
        "user"=>$user,
        "apply"=>$apply,
        "teacher"=>$teacher,
        "course"=>Course::all()
      );
      return view("studentinfo",$data);
    }
   /*
    * 录取查询
    */
    public function search(Request $request){
      $apply = "";
      $data="";
      if($request->isPost()) {
        $data = $request->post();
        $res = User::where(["name" => $data['name'], "idcard" => $data['idcard']])->find();
        $apply = Apply::where(['userid' => $res['id']])->select();
      }
      $data = array(
        "setting" => $this->getAllSetting(),
        "apply"=>$apply,
        "data"=>$data
      );
      return view("search",$data);
    }

    /*
     * 须知
     */
    public function know(Request $request){
      $k = $request->get("k");
      $res= Text::where(["k"=>$k])->find();
      $data = array(
        "setting" => $this->getAllSetting(),
        'text'=>$res
      );
      return view("know",$data);
    }

    /*
     * 我的老师
     */
    public function myteacher(){
      $teacher = $this->user_info();
      $my_teacher = collection(Teacher::where(["pid"=>$teacher['id']])->select())->toArray(); //查询自己组的老师
      $arr = [
        "setting" => $this->getAllSetting(),
        "my_teacher" => $my_teacher,
      ];
      return view("myteacher",$arr);
    }
    /*
     * 查询是否有老师
     */
    public function get_my_teacher_count(){
      $teacher = $this->user_info();
      $count =  Teacher::where("pid",$teacher["id"])->count("*");
      return json(['c'=>$count]);
    }
    /*
     * 我的微信二维码
     */
    public function  qrcode(){
      $teacher = $this->user_info();
      $arr = [
        "setting"=>$this->getAllSetting(),
        "teacher"=>$teacher,
      ];
      return view("qrcode",$arr);
    }
    /*
     * 我的微信二维码
     */
    public function  qr_src(){
      $teacher = $this->user_info();
      $arr = [
        "setting"=>$this->getAllSetting(),
        "teacher"=>$teacher,
      ];
      return view("qr_src",$arr);
    }
    /*
     * 不同类别的学生
     */
    public function student_type(Request $request){
      $user = $this->user_info(); //当前登录的老师
      $id = $request->get("id");
      $status = $request->get("type");
      //如果接收的老师的id与登录的用户id不一致就查询用户的信息
      $th='';
      if($user['id'] != $id){
        $th = Teacher::where("id",$id)->field("name")->find();
      }
      $Applys = collection(Apply::where(["teacher_id"=>$id,"grade"=>date("Y",time()),"status"=>$status])->order("create_time","desc")->select())->toArray();
      foreach($Applys as $key=>$val){
        $Applys[$key]['user']=User::where(["id"=>$val['userid']])->find()->toArray();
      }
      $status_name=["","缴费待审核","已录取","拒绝","待支付","信息待审核"];
      $data = array(
        "setting" => $this->getAllSetting(),
        "apply" => $Applys,
        "user"=>$user,
        "th"=>$th,
        "type_name"=>$status_name[$status]
      );
      return view("student_type", $data);
    }
    /*
     * 修改学生的状态为通过
     */
    public function classAddStudent2(Request $request)
    {
      $data = $request->post();
      $data['type'] = 4;
      $data['status'] = 4;
      if (Apply::update($data)) {
        $apply = Apply::get($data['id']);
        $student = User::get($apply['userid']);
        $res = message($student['phone'],["name"=>$student['name'],"major"=>$apply['major']],2); //发送录取通知短信
        $msg_msg ="报名成功";
        if($res['stat'] == 100){
          $msg_msg .= ",短信发送：".$res['message'];
        }else{
          $msg_msg .= ",短信发送失败：".$res['message'];
        }
        $this->success($msg_msg, "My/studentinfo");
      } else {
        $this->error("报名失败");
      }
    }
    /*
     * 修改学生的专业
     */
    public function changeStudentMajor(Request $request){
      $data = $request->post();
      if(Apply::update($request->post())){
          $this->success("保存成功");
      }else{
          $this->error("保存失败");
      }
    }


}
